package com.service1.controller

import com.base.model.service1.Model_Service1
import com.base.model.service1.Model_User
import com.base.model.service1.db.Model_Service1_Redis
import com.base.model.service2.Model_Service2
import com.base.service.IRPC_Service1_Feign
import com.service1.db.mongodb.service.MongoDBService
import com.service1.db.mybatis.db1.service.MyBatisDB1Service
import com.service1.db.mybatis.db2.service.MyBatisDB2Service
import com.service1.db.mybatis_plus.db1.service.MyBatisPlusDB1Service
import com.service1.db.mybatis_plus.db2.service.MyBatisPlusDB2Service
import com.service1.db.redis.RedisService
import com.service1.rpc.RPC_Service2_Feign
import com.yfree.global.YController
import io.seata.spring.annotation.GlobalTransactional
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.web.bind.annotation.RestController

@RefreshScope
@RestController
class Controller_Service1(
    @Value("\${server.port}") var serverPort: String,
    var myBatisDB1Service: MyBatisDB1Service,
    var myBatisDB2Service: MyBatisDB2Service,
    var myBatisPlusDB1Service: MyBatisPlusDB1Service,
    var myBatisPlusDB2Service: MyBatisPlusDB2Service,
    var redisService: RedisService,
    var mongoDBService: MongoDBService,
    var rpc_Service2_Feign: RPC_Service2_Feign
) : YController(), IRPC_Service1_Feign {
    @GlobalTransactional
    override fun callService2(): String {
        val yModel = yFromRPC(rpc_Service2_Feign.index(), Model_Service2::class.java, "rpc_Service2.index调用失败")
        return yResult(
            result = listOf(
                Model_Service1("Feign:Server1Port:$serverPort,${yModel.root.result[0].value}")
            )
        )
    }

    @GlobalTransactional
    override fun db_MySQL(id: String): String {
        return yResult(result = myBatisDB1Service.find(id) + myBatisDB2Service.find(id))
    }

    @GlobalTransactional
    override fun db_MySQL_MP(id: String): String {
        return yResult(
            result = listOf(
                myBatisPlusDB1Service.find(id),
                myBatisPlusDB2Service.find(id)
            )
        )
    }

    @GlobalTransactional
    override fun db_MongoDB(id: String): String {
        return yResult(
            result = listOf(
                mongoDBService.find(id)
            )
        )
    }

    @GlobalTransactional
    override fun db_Redis(id: String): String {
        redisService.saveID(id)
        return yResult(
            result = listOf(
                Model_Service1_Redis(redisService.readID()),
                Model_Service1_Redis(redisService.readSomething())
            )
        )
    }

    @GlobalTransactional
    override fun do_feign(doException: String): String {
        print("开始更新")
        if (!myBatisDB1Service.update()) return yResult(code = -1, message = "myBatisDB1Service更新失败")
        if (!myBatisDB2Service.update()) return yResult(code = -1, message = "myBatisDB2Service更新失败")
        print("更新完成")
        if (doException == "1") 1 / 0 // 制造异常触发回滚
        return yResult()
    }

    @GlobalTransactional
    override fun getTokenByUid(uid: String): String {
        val userModel = Model_User()
        userModel.token = "${uid}_token"
        return yResult(result = listOf(userModel))
    }
}
